package main

import (
	"errors"
	"fmt"
	"math"
)

func Sqrt(n float64) (float64, error) {
	if n < 0 {
		return 0, errors.New("不可以小于0")
	}
	precision := 0.00001
	max := n / 2
	min := 0.000
	mid := max
	for i := 1; ; i++ {
		oMid := mid
		if mid*mid > n {
			max = (max-min)/2 + min
			mid = min
		} else if mid*mid < n {
			max = (max-min)/2 + max
			min = mid
			mid = max
		} else {
			break
		}

		if param := math.Max(math.Abs(oMid-mid), precision); param == precision {
			break
		}

	}
	return mid, nil
}

func main() {
	fmt.Println(Sqrt(4))
}
